﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TRec.BusinessLayer;

namespace TRec.Windows.UI.UserControls
{
    public partial class TimeEditGrid : UserControl
    {
        public TimeEditGrid()
        {
            InitializeComponent();
        }

        private User m_currUser = null;
        public event FormClosingEventHandler EditFormClosing;

        public void SetupGrid( List<TimeEntry> items, User currUser )
        {
            m_currUser = currUser;
            dgHistory.DataSource = items;
            SetupContext();
        }


        private void ShowEdit( TimeEntry currItem, TimeEntry prevItem, TimeEntry nextItem )
        {
            frmEditTime tmEdit = new frmEditTime();
            tmEdit.PopulateScreen( currItem, prevItem, nextItem, m_currUser );
            tmEdit.StartPosition = FormStartPosition.CenterParent;
            tmEdit.BringToFront();
            tmEdit.FormClosing += new FormClosingEventHandler( tmEdit_FormClosing );
            tmEdit.ShowDialog( this );
        }

        private void dgHistory_DoubleClick( object sender, EventArgs e )
        {
            if ( dgHistory.SelectedRows.Count > 0 )
            {
                int selectedIndex = dgHistory.Rows.IndexOf( dgHistory.SelectedRows[ 0 ] );
 
                TimeEntry currItem = (TimeEntry)dgHistory.SelectedRows[ 0 ].DataBoundItem;
                TimeEntry prevItem = selectedIndex < dgHistory.Rows.Count - 1 ? (TimeEntry)dgHistory.Rows[ selectedIndex + 1 ].DataBoundItem : null;
                TimeEntry nextItem = selectedIndex > 0 ? (TimeEntry)dgHistory.Rows[ selectedIndex - 1 ].DataBoundItem : null;
                ShowEdit( currItem, prevItem, nextItem );
            }
        }

        private void editToolStripMenuItem_Click( object sender, EventArgs e )
        {
            if ( dgHistory.SelectedRows.Count > 0 )
            {
                int selectedIndex = dgHistory.Rows.IndexOf( dgHistory.SelectedRows[ 0 ] );

                TimeEntry currItem = (TimeEntry)dgHistory.SelectedRows[ 0 ].DataBoundItem;
                TimeEntry prevItem = selectedIndex < dgHistory.Rows.Count - 1 ? (TimeEntry)dgHistory.Rows[ selectedIndex + 1 ].DataBoundItem : null;
                TimeEntry nextItem = selectedIndex > 0 ? (TimeEntry)dgHistory.Rows[ selectedIndex - 1 ].DataBoundItem : null;
                ShowEdit( currItem, prevItem, nextItem );
            }
        }

        private void insertAboveToolStripMenuItem_Click( object sender, EventArgs e )
        {
            int selectedIndex = dgHistory.Rows.IndexOf( dgHistory.SelectedRows[ 0 ] );

            TimeEntry currItem = new TimeEntry();
            TimeEntry prevItem = (TimeEntry)dgHistory.SelectedRows[ 0 ].DataBoundItem;
            TimeEntry nextItem = selectedIndex > 0 ? (TimeEntry)dgHistory.Rows[ selectedIndex - 1 ].DataBoundItem : null;

            currItem.StartDateTime = prevItem.EndDateTime;
            currItem.EndDateTime = nextItem == null ? currItem.StartDateTime : nextItem.StartDateTime;

            ShowEdit( currItem, prevItem, nextItem );
        }

        private void insertBelowToolStripMenuItem_Click( object sender, EventArgs e )
        {
            int selectedIndex = dgHistory.Rows.IndexOf( dgHistory.SelectedRows[ 0 ] );

            TimeEntry currItem = new TimeEntry();
            TimeEntry prevItem = selectedIndex < dgHistory.Rows.Count - 1 ? (TimeEntry)dgHistory.Rows[ selectedIndex + 1 ].DataBoundItem : null;
            TimeEntry nextItem = (TimeEntry)dgHistory.SelectedRows[ 0 ].DataBoundItem;

            currItem.EndDateTime = nextItem.StartDateTime;
            currItem.StartDateTime = prevItem == null ? currItem.EndDateTime : prevItem.EndDateTime;

            ShowEdit( currItem, prevItem, nextItem );
        }


        void tmEdit_FormClosing( object sender, FormClosingEventArgs e )
        {
            if ( EditFormClosing != null )
            {
                EditFormClosing( sender, e );
            }
        }

        private void dgHistory_SelectionChanged( object sender, EventArgs e )
        {
            SetupContext();
        }

        private void SetupContext()
        {
            //ensure we only show relevant context menu items
            editToolStripMenuItem.Enabled = true;
            insertAboveToolStripMenuItem.Enabled = true;
            insertBelowToolStripMenuItem.Enabled = true;

            if ( dgHistory.SelectedRows.Count <= 0 )
            {
                //nothing selected, all are disabled except
                editToolStripMenuItem.Enabled = false;
                insertAboveToolStripMenuItem.Enabled = false;
                insertBelowToolStripMenuItem.Enabled = false;
            }
            else
            {
                //something is selected
                TimeEntry currItem = (TimeEntry)dgHistory.SelectedRows[ 0 ].DataBoundItem;
                int selectedIndex = dgHistory.Rows.IndexOf( dgHistory.SelectedRows[ 0 ] );


                if ( selectedIndex < dgHistory.Rows.Count - 1 )
                {
                    //there is an entry under (before) this one
                    TimeEntry previousItem = (TimeEntry)dgHistory.Rows[ selectedIndex + 1 ].DataBoundItem;
                    if ( previousItem.EndDateTime >= currItem.StartDateTime )
                    {
                        insertBelowToolStripMenuItem.Enabled = false;
                    }
                }
                
                if ( selectedIndex > 0 )
                {
                    //there is an entry above (after) this one
                    TimeEntry nextItem = (TimeEntry)dgHistory.Rows[ selectedIndex - 1 ].DataBoundItem;
                    if ( nextItem.StartDateTime <= currItem.EndDateTime )
                    {
                        insertAboveToolStripMenuItem.Enabled = false;
                    }
                }
            }
        }

        private void addNewToolStripMenuItem_Click( object sender, EventArgs e )
        {
            TimeEntry currItem = new TimeEntry();
            TimeEntry prevItem = null;
            TimeEntry nextItem = null;

            currItem.EndDateTime = OperationsReadOnly.RoundToMinute( DateTime.Now );
            currItem.StartDateTime = OperationsReadOnly.RoundToMinute( DateTime.Now );

            ShowEdit( currItem, prevItem, nextItem );
        }

    }
}
